<?php
/**
 * WpSmartyView.php
 * 
 * This file contains the WpSmartyView class, a wrapper for the Smarty template engine 
 * to be used in WildPack mvc
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage mvc.view
 */

/**
 * The interface to implement
 */
require_once (dirname(__FILE__).'/IWpView.php');

/**
 * This is a wrapper for the Smarty template engine to be used as view 
 * in WildPack mvc
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage mvc.view
 */
class WpSmartyView implements IWpView
{
	/**
	 * @var The smarty object
	 */
	private $_sm = null;
	
	/**
	 * The extension of the view files
	 */
	private $_viewExtension = '.tpl';
	
	/**
	 * @var The default view which is used if no view was specified
	 */
	public $DefaultView = 'Index';
	
	/**
	 * @var The name of the view to use
	 */
	public $Viewname = 'Index';
	
	/**
	 * The class constructor
	 */
	public function __construct()
	{
		$this->_sm = new Smarty();
	}

	/**
   * Assigns data to the view
   * @param mixed $data The data to assign to the view
   */
	public function assign($data)
	{
		foreach ($data as $key => $dentry) {
			$this->_sm->assign($key, $dentry);
		}
	}
	
	/**
   * Renders the view and returns the content
   * @param object $viewname[optional] The name of the view to render
   * @return string The rendered view as string
   */
	public function fetch($viewname = '')
	{
		if (is_null($viewname) || $viewname === '') {
			$viewname = $this->DefaultView;
		}
		
		return $sm->fetch($viewname.'.'.$this->_viewExtension);
	}
	
	/**
   * Renders the view
   * @param string $viewname The name of the view to render
   * @param object $response The response to write the rendered view to
   */
	public function render($response)
	{
		if (is_null($this->Viewname) || $this->Viewname === '') {
			$this->Viewname = $this->DefaultView;
		}
		
		$body = $this->_sm->fetch($this->Viewname.$this->_viewExtension);
		$response->write($body);
		$response->flush();
	}
	
	/**
   * Specifies the name of the view to use
   * @param string $viewname The name of the view to use
   */
	public function setViewname($viewname)
	{
		$this->Viewname = $viewname;
	}
}

/* End of file WpSmartyView.php */
/* Location: ./wildpack/mvc/view/WpSmartyView.php */
